package com.mktech.taraservicelib.http.customRequest;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.mktech.taraservicelib.entity.homefetch.HomeFetchBean;

public class HomeFetchBeanRequest extends Request<HomeFetchBean>
{
	private Listener<HomeFetchBean> mListener;
	
	public HomeFetchBeanRequest(int method, String url, Listener<HomeFetchBean> listener, ErrorListener errorListener)
	{
		super(method, url, errorListener);
		mListener = listener;
	}

	public HomeFetchBeanRequest(String url, Listener<HomeFetchBean> listener, ErrorListener errorListener)
	{
		this(Method.GET, url, listener, errorListener);
	}

	@Override
	protected void deliverResponse(HomeFetchBean response)
	{
		// TODO Auto-generated method stub
		mListener.onResponse(response);
	}

	@Override
	protected Response<HomeFetchBean> parseNetworkResponse(NetworkResponse response)
	{
		// TODO Auto-generated method stub
		try
		{
			String string = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
			string = string.replace("[],", "");
			Gson gson = new Gson();
			Type type = new TypeToken<HomeFetchBean>(){}.getType();
			return Response.success((HomeFetchBean)gson.fromJson(string, type), HttpHeaderParser.parseCacheHeaders(response));
		}
		catch (UnsupportedEncodingException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			Response.error(new ParseError(e));
		}
		return null;
	}
	
}

